#include "headfiles.h"

using namespace std;

int main(int argc, char **argv) {
    if (argc != 4) {
        cout << "Usage: " << argv[0] << " file1 file2 K" << endl;
        return 0;
    }

    int k = atoi(argv[3]);

    fstream fs;
    fs.open(argv[1], iostream::in);
    if (!fs.is_open()) {
        cout << "File " << argv[1] << " open error!" << endl;
        return 1;
    }

    vector<int> vec;
    string str;
    while (fs >> str) {
        if (str.empty()) {
            break;
        }

        int val = atoi(str.c_str());
        vec.push_back(val);
    }
    fs.close();

    int m = vec.size();
    int *a = new int[m];
    int i;
    for (i = 0; i < m; i++) {
        a[i] = vec[i];
    }
    vec.clear();

    fs.open(argv[2], iostream::in);
    if (!fs.is_open()) {
        cout << "File " << argv[2] << " open error!" << endl;
        return 1;
    }

    while (fs >> str) {
        if (str.empty()) {
            break;
        }

        int val = atoi(str.c_str());
        vec.push_back(val);
    }
    fs.close();

    int n = vec.size();
    int *b = new int[n];
    for (i = 0; i < n; i++) {
        b[i] = vec[i];
    }
    vec.clear();

    int elem;
    bool res = calcKthElem(a, m, b, n, k, elem);

    if (res) {
        cout << k << "th elem is " << elem << endl;
    }
    else {
        cout << k << "th elem does not exist!" << endl;
    }

    return 0;
}

